<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LoyaltyCard extends CI_Controller{

	public function index()
	{
		$this->load->helper('form');
		$this->load->model('promotion');
		$this->load->model('client');
		
		$dataa = array();
		$in = 0;
		foreach($this->client->getClient() as $client)
		{
			$pro['client'] = $client->client;
			$pro['email'] = $client->email;
			$pro['id'] = $client->id;
			$dataa[$in] = $pro;
			$in++;
		}
		
		$promo;
		$promoCode;
		foreach($this->promotion->getPromotion() as $prod)
		{
			$promo[$prod->id] = $prod->name;
			$promoCode[$prod->id] = $prod->code;
		}
		
		$data['clients'] = $dataa;
		$data['promotions'] = $promo;
		$data['code'] = $promoCode;
		$this->load->view('client', $data);
		
	}
	
	public function emailSent()
	{
		$this->load->view('emailSentToClient');
	}
	
	public function process($em = "lukasz.iwanski@gmail.com")
	{
		$this->load->helper('url');
		$this->load->library('email');
		$this->email->from('shop@deep-beta.co.uk', 'Promotion!');
		$this->email->to($em); 
		
		$this->email->subject('Congratulation!');
		$this->email->message('
		Dear '.$_POST['client'].'	
		With this code: '.$_POST['code'].'
		you can claim your promotion: '.$_POST['promotion']);	

		$this->email->send();
		echo "<pre>";
			echo $this->email->print_debugger();
		echo "</pre>";
		//update the product
		redirect('/loyaltyCard/emailSent', 'refresh');
	}
}

